<!--电子围栏页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">围栏管理</span>
    <el-button type="primary" style="float:right;margin-top:13px;">新建围栏</el-button>
  </div>
  <!-- 围栏查询卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="围栏名称">
        <el-input placeholder="请输入" style="width:220px;"
                  v-model="geoSearchForm.name" @keydown.enter="loadGeo"></el-input>
      </el-form-item>
      <el-form-item label="围栏状态" style="width:290px;">
        <el-select placeholder="请选择" v-model="geoSearchForm.status" @change="loadGeo">
          <el-option label="启用" value="1"/>
          <el-option label="禁用" value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="reset()">重置</el-button>
        <el-button type="primary" @click="loadGeo()">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 围栏表格数据 -->
  <el-card style="margin:20px;">
    <el-table stripe :data="geoArr">
      <el-table-column label="编号" prop="id" type="index" align="center" width="100"></el-table-column>
      <el-table-column label="围栏名称" prop="name" align="center"></el-table-column>
      <el-table-column label="围栏坐标点" prop="position" align="center" show-overflow-tooltip="true" width="500"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="围栏状态" align="center">
        <template #default="scope">
          <el-switch inactive-value="0" active-value="1" v-model="scope.row.status"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="loadVehicle(scope.row.id)">管理车辆</el-button>
          <el-button type="primary" link @click="deleteGeo(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 已绑定车辆弹窗 -->
  <el-dialog title="已绑定车辆" v-model="bindVehicleDialogVisible" style="padding:40px;">
    <el-button type="primary" style="margin:10px 0;" @click="showUnbindVehicleList()">添加绑定车辆</el-button>
    <el-table :data="bindVehicleArr">
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center"></el-table-column>
      <el-table-column
          label="操作"
          fixed="right"
          width="200"
          align="center">
        <template #default="scope">
          <el-button link type="primary" @click="removeBindVehicle(scope.row.id)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
  <!-- 添加可绑定车辆列表弹窗 -->
  <el-dialog title="可绑定车辆列表" v-model="AddBindVehicleDialogVisible" :before-close="handlerCloseInner">
    <el-table :data="unBindvehicleArr">
      <el-table-column label="编号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center"></el-table-column>
      <el-table-column prop="status" label="车辆状态" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="bindNewVehicle(scope.row.id)">绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>
<script setup>
import {onMounted, ref} from "vue";
import qs from 'qs';
import axios from "axios";
import {ElMessage} from "element-plus";

//控制"已绑定车辆弹窗"是否显示
const bindVehicleDialogVisible = ref(false);
//控制"可绑定车辆列表"是否显示
const AddBindVehicleDialogVisible = ref(false);

//定义响应式form来双向绑定前端输入框输入的数据
const geoSearchForm = ref({
  name:'',
  status:''
})
//定义数组接收后端传递来的围栏List
const geoArr = ref([]);

//定义加载电子围栏的方法
const loadGeo = ()=>{
  let data = qs.stringify(geoSearchForm.value);
  console.log(data)
  axios.get(BASE_URL + "/v1/geofence/select?" + data)
      .then((response)=>{
        if(response.data.code == 2000){
          console.log("查询成功");
          geoArr.value = response.data.data;
          console.log(geoArr);
        }else {
          ElMessage.error(response.data.msg)
        }
      })
};
//定义删除某个电子围栏的方法
const deleteGeo = (geofenceId)=>{
  if (confirm("确定要删除围栏吗?")){
    console.log(geofenceId);
    axios.post(BASE_URL + '/v1/geofence/delete?id='+geofenceId)
        .then((response)=>{
          if (response.data.code == 2000){
            ElMessage.success("删除围栏成功!");
            loadGeo();
          }else {
            ElMessage.error(response.data.msg);
          }
        })
  }
}
onMounted(()=>{
  loadGeo();
})
//定义重置按钮功能
const reset = ()=>{
  geoSearchForm.value = {};
  loadGeo();
};

//TODO定义一个全局变量的geofenceId用来在解绑车辆和   中使用
var geoId = null;

//定义数组用来保存当前围栏已绑定的车辆信息
const bindVehicleArr = ref([]);
//定义加载当前围栏上绑定车辆的数据
const loadVehicle = (geofenceId)=>{
  geoId = geofenceId;
  //显示当前围栏绑定车辆对话框
  bindVehicleDialogVisible.value = true;
  //向车辆后端地址发送请求，根据geofenceId来查询需要的的车辆
  axios.get(BASE_URL + '/v1/vehicle/select?geofenceId=' + geofenceId)
      .then((response)=>{
        if(response.data.code == 2000){
          bindVehicleArr.value = response.data.data;
          console.log("绑定车辆的信息",bindVehicleArr.value)
        }else {
          ElMessage.error(response.data.msg)
        }
      })
}
//移出已经绑定的车辆
const removeBindVehicle = (id)=>{
  // console.log(typeof vehicleId)
    axios.post(BASE_URL + '/v1/vehicle/unbind/' + id)
        .then((response)=>{
          if(response.data.code == 2000){
            ElMessage.success("移除车辆成功!")
            loadVehicle(geoId);
          }else {
            ElMessage.error(response.data.msg)
          }
        })
}

//vehicleTable
const unBindvehicleArr = ref([]);
//添加围栏上可绑定车辆的列表
const showUnbindVehicleList = ()=>{
  AddBindVehicleDialogVisible.value = true;
  axios.get(BASE_URL + '/v1/vehicle/select?geofenceBindStatus=0')
      .then((response)=>{
        if(response.data.code == 2000){
          unBindvehicleArr.value = response.data.data;
        }else {
          ElMessage.error(response.data.msg)
        }
      })
}

//定义围栏上新增绑定车辆的方法
const bindNewVehicle = (vehicleId)=>{
  console.log("绑定! vehicleId " , vehicleId)
  axios.post(BASE_URL + '/v1/vehicle/bind/'+ geoId + '/' + vehicleId)
      .then((response)=>{
        if(response.data.code == 2000){
          ElMessage.success("绑定成功!")
          //每成功绑定走一辆车,就需要重新加载所有可绑车辆
          showUnbindVehicleList();
          //每绑定一辆车,就需要重新加载该围栏上绑定的车辆,但调用次数太多,开销较大(在这里根本看不到外面的数据一直刷新外面的数据根本没有用)
          //loadVehicle(geoId);
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}

const handlerCloseInner = ()=>{
  //（before-close 只会在用户点击关闭按钮或者对话框的遮罩区域时被调用
  // 关闭内层窗口后刷新外层的数据
  loadVehicle(geoId)
  //把内层窗口设置为不显示
  AddBindVehicleDialogVisible.value = false;
}

</script>

<style scoped>
</style>